module(
    name = "rules_wasm",
    version = "1.1.1",
)

bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "rules_rust", version = "0.57.1")

bazel_dep(name = "buildifier_prebuilt", version = "8.0.1", dev_dependency = True, repo_name = "buildtools")

# The example module (which contains basic tests)
# is itself referenced by tests in the main module,
# which verify that some example tests are running properly.
bazel_dep(name = "example", dev_dependency = True)
local_path_override(
    module_name = "example",
    path = "example",
)

# Supported execution platforms for pre-built binaries.
# TODO: Figure out how to dedupe this list with `private.bzl`.
execution_platforms = [
    "aarch64-linux",
    "aarch64-macos",
    "x86_64-linux",
    "x86_64-macos",
]

# Version for WebAssembly Compositions: https://github.com/bytecodealliance/wac/releases.
wac_version = "0.6.1"

wac_integrity = {
    "aarch64-linux": "sha384-a/w+0nesiZRqMqid2W9mrjBA4S/qU4iGXRgHZjA+3Ydq7/efLT3XE41mGBNNqkeC",
    "aarch64-macos": "sha384-cEtDDp4io01CLGUuZVZu9LPbk3/Ib7yYn88Q0XG785OltXG81NibGfZVSD9C/+n/",
    "x86_64-linux": "sha384-90D0wJeI35nuWmkZMHrOd79pVequX7kbAWHqpcmsG6ZKJdwmfLZVTSGyuvPhjlpo",
    "x86_64-macos": "sha384-+hH0YcJEeLeih4NULigzzFgJ3pCKnKq70oB5hEJGKC0E+xU4Hd/kH6NUV9BX/IV+",
}

# Version for WASI interface definitions: https://github.com/WebAssembly/WASI/releases.
wasi_version = "0.2.3"

wasi_integrity = "sha384-wd/5mo2TDA7ADdkZqib/2gNCGHIghMCM8ONJNq/xK9WVznX6EU4iW4jbJKYxXu/7"

# Version for the WASI SDK: https://github.com/WebAssembly/wasi-sdk/releases.
wasi_sdk_version = "25"

wasi_sdk_integrity = {
    "aarch64-linux": "sha384-5bnm2bABYAmVF0KeEgz3SAbo4qKRQJkByW9eQZ6KIqV+BF2KiZEEjKI4+ELdJPqH",
    "aarch64-macos": "sha384-T6tTh2QN9bfrKptJ80vGCNoyvRbcXHEkzP805V9THXkSFsQD/fwHYkOWD7VOf61f",
    "x86_64-linux": "sha384-2hliAV3H1pxMMROU9BarleLEH0dng/XdvfpSsGJKbvRvyy98K59WD8fN5RrvaCML",
    "x86_64-macos": "sha384-zVs1lhkofsVL6pWDsKlFEPjWeMxmH19tQX3a4jhfO1XbLlga7Rw/pYQvfReiY7AU",
}

# Version for wasmtime: https://github.com/bytecodealliance/wasmtime/releases.
wasmtime_version = "28.0.0"

wasmtime_integrity = {
    "aarch64-linux": "sha384-e4eOQdu/NRb2d33CGqO131rUcwJ0bgZVGFDCxrrQHEHaUO/km9K0qFa1zT8I/0or",
    "aarch64-macos": "sha384-4nFcDlSpEKq/pVrKmOOqwArwYnjJl3M/+Nk9Jp9kmQBUs0jdvvLn69ulvOCoKBg0",
    "x86_64-linux": "sha384-PwpaM4kG4e723IcNvwXTf5onABRQMXsNge2bOd1GumzrxO2ngXfc/Qw604bOSFKU",
    "x86_64-macos": "sha384-wVbcHFcVW/QLidoX4jphwgbGEDYAd5VjLPmSIOS4fTf7E4p8aA7tG0t3+JKkTxlN",
}

# Version for `wasm-tools`: https://github.com/bytecodealliance/wasm-tools/releases.
wasm_tools_version = "1.222.0"

wasm_tools_integrity = {
    "aarch64-linux": "sha384-TVvUEfaZld5M/ieispvUCQKz/4p3cj6veYQOjnxjw1sW1ru9vBcaGOCNDGCEhbIw",
    "aarch64-macos": "sha384-qHhU42lAl13p3CCIE1r4xPqt4jIyyXqBX1Eg9Ede7OvliwTnsl1yooODLu+yxgDp",
    "x86_64-linux": "sha384-27Bnt7XZaY+o11oPSuIqqbqCkPsbq4fXbh2qxV+gxpNpFJ88USjrnNobRjFBvK3z",
    "x86_64-macos": "sha384-71HFBpP7i/19RB/2ZMAR4RdSUPgOOmlE72ucVZug6BFfTb2wEll19f0OQZwpo1VC",
}

# Version for `wit-bindgen`: https://github.com/bytecodealliance/wit-bindgen/releases.
wit_bindgen_version = "0.36.0"

wit_bindgen_integrity = {
    "aarch64-linux": "sha384-dWeeLwRPUDle2+UIC341EZKp9eTwmaHEmcgz5jNeLCCJWazQDKMYj+v7RD+OqDJj",
    "aarch64-macos": "sha384-HECPlVnFj5HTjEvseZciO5FE66mm+3Z0QlTUrcM0ycjI4PpKd5C15MFQ8xY3NuhB",
    "x86_64-linux": "sha384-LL0oARTtBo6pdTOmJF7OoqIpv+O/L0zQ1ltubxgOTzEvkEpjuLTUE1RziPFfJGB5",
    "x86_64-macos": "sha384-eKzFLPVUMtww4ov1jEzaK31iE0vvMQ22t9xvcus86+mpq8esw43s1xm0naPivEGo",
}

# Rust setup: wit-bindgen-generated Rust files depend on the wit-bindgen crate.
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate.spec(
    package = "wit-bindgen",
    version = wit_bindgen_version,
)
crate.from_specs(name = "rules-wasm-crates")
use_repo(crate, "rules-wasm-crates")

# Manually download prebuilt binaries for various dependencies:

http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")

wac_platforms = {
    "aarch64-linux": "aarch64-unknown-linux-musl",
    "aarch64-macos": "aarch64-apple-darwin",
    "x86_64-linux": "x86_64-unknown-linux-musl",
    "x86_64-macos": "x86_64-apple-darwin",
}

[http_file(
    name = "wac-" + exe_platform,
    executable = True,
    integrity = wac_integrity[exe_platform],
    url = "https://github.com/bytecodealliance/wac/releases/download/v{version}/wac-cli-{platform}".format(
        # WAC uses non-canonical platform strings in the URLs:
        platform = wac_platforms[exe_platform],
        version = wac_version,
    ),
) for exe_platform in execution_platforms]

http_archive(
    name = "wasi",
    build_file_content = """
load(":wasm.bzl", "wasi_packages")

[filegroup(
    name = package,
    srcs = glob(["WASI-{version}/wasip2/" + package + "/*.wit"]),
    visibility = ["//visibility:public"],
) for package in wasi_packages.keys()]
""".format(version = wasi_version),
    integrity = wasi_integrity,
    # Ideally there would be something like `remote_file_urls` for local files
    # so we can eliminate this redundant patch file
    # and instead just copy `wasm.bzl` into the remote repo:
    # https://github.com/bazelbuild/bazel/issues/24499.
    patches = [":wasi-patch.bzl"],
    url = "https://github.com/WebAssembly/WASI/archive/refs/tags/v{version}.tar.gz".format(version = wasi_version),
)

wasi_sdk_platforms = {
    "aarch64-linux": "arm64-linux",
    "aarch64-macos": "arm64-macos",
    "x86_64-linux": "x86_64-linux",
    "x86_64-macos": "x86_64-macos",
}

[http_archive(
    name = "wasi-sdk-" + exe_platform,
    # Note: these are symlinks within the HTTP archive:
    # - clang -> clang-19
    # - ar -> llvm-ar
    # - nm -> llvm-nm
    # - objdump -> llvm-objdump
    # - strip -> llvm-objcopy
    build_file_content = """
load("@bazel_skylib//rules/directory:directory.bzl", "directory")
load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory")

package(default_visibility=["//visibility:public"])

alias(name = "clang", actual = "wasi-sdk-{version}.0-{platform}/bin/clang")
alias(name = "wasm-component-ld", actual = "wasi-sdk-{version}.0-{platform}/bin/wasm-component-ld")
alias(name = "ar", actual = "wasi-sdk-{version}.0-{platform}/bin/ar")
alias(name = "nm", actual = "wasi-sdk-{version}.0-{platform}/bin/nm")
alias(name = "objdump", actual = "wasi-sdk-{version}.0-{platform}/bin/objdump")
alias(name = "strip", actual = "wasi-sdk-{version}.0-{platform}/bin/strip")

directory(
    name = "lib",
    srcs = glob(["wasi-sdk-{version}.0-{platform}/lib/**"]),
)
subdirectory(
    name = "include",
    parent = ":lib",
    path = "clang/19/include",
)
""".format(
        platform = wasi_sdk_platforms[exe_platform],
        version = wasi_sdk_version,
    ),
    integrity = wasi_sdk_integrity[exe_platform],
    url = "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-{version}/wasi-sdk-{version}.0-{platform}.tar.gz".format(
        platform = wasi_sdk_platforms[exe_platform],
        version = wasi_sdk_version,
    ),
) for exe_platform in execution_platforms]

[http_archive(
    name = "wasmtime-" + exe_platform,
    build_file_content = """
alias(
    name = "wasmtime",
    actual = "wasmtime-v{version}-{platform}/wasmtime",
    visibility = ["//visibility:public"],
)
""".format(
        platform = exe_platform,
        version = wasmtime_version,
    ),
    integrity = wasmtime_integrity[exe_platform],
    url = "https://github.com/bytecodealliance/wasmtime/releases/download/v{version}/wasmtime-v{version}-{platform}.tar.xz".format(
        platform = exe_platform,
        version = wasmtime_version,
    ),
) for exe_platform in execution_platforms]

[http_archive(
    name = "wasm-tools-" + exe_platform,
    build_file_content = """
alias(
    name = "wasm-tools",
    actual = "wasm-tools-{version}-{platform}/wasm-tools",
    visibility = ["//visibility:public"],
)
""".format(
        platform = exe_platform,
        version = wasm_tools_version,
    ),
    integrity = wasm_tools_integrity[exe_platform],
    url = "https://github.com/bytecodealliance/wasm-tools/releases/download/v{version}/wasm-tools-{version}-{platform}.tar.gz".format(
        platform = exe_platform,
        version = wasm_tools_version,
    ),
) for exe_platform in execution_platforms]

[http_archive(
    name = "wit-bindgen-" + exe_platform,
    build_file_content = """
alias(
    name = "wit-bindgen",
    actual = "wit-bindgen-{version}-{platform}/wit-bindgen",
    visibility = ["//visibility:public"],
)
""".format(
        platform = exe_platform,
        version = wit_bindgen_version,
    ),
    integrity = wit_bindgen_integrity[exe_platform],
    url = "https://github.com/bytecodealliance/wit-bindgen/releases/download/v{version}/wit-bindgen-{version}-{platform}.tar.gz".format(
        platform = exe_platform,
        version = wit_bindgen_version,
    ),
) for exe_platform in execution_platforms]

http_file(
    name = "libwit-bindgen-cabi-realloc",
    integrity = "sha384-4cpApVpbtIZb+jzbpZaSQ2Td0SrcvhkGq4qFwruq4o+OrvlPcnkaZ1Alur8Bd/Ca",
    url = "https://github.com/bytecodealliance/wit-bindgen/raw/v{version}/crates/guest-rust/rt/src/libwit_bindgen_cabi_realloc.a".format(version = wit_bindgen_version),
)

# Temporary crutch until we can build against wasip2 directly.
http_file(
    name = "wasi-snapshot-preview1-reactor",
    integrity = "sha384-c0P96LoyKwdtEo+9rHgzfS0Tb8Qva2is/g3vHfpUA1Sq5Mdcadg+pRSufZ2ZYn74",
    url = "https://github.com/bytecodealliance/wasmtime/releases/download/v{version}/wasi_snapshot_preview1.reactor.wasm".format(version = wasmtime_version),
)

register_toolchains("//:wasm32-wasi-toolchain")
